import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { useState, useRef, useEffect } from "react";
/**
 * 合并对象,如果值相同返回原对象
 *
 * @export
 * @template T
 * @param {T} prevObj
 * @param {Partial<T>} partialObj
 * @returns {T}
 */ export function concatObj(prevObj, partialObj) {
    var newObj = prevObj;
    for(var key in partialObj){
        if (Object.prototype.hasOwnProperty.call(newObj, key)) {
            var element = partialObj[key];
            if (newObj[key] !== element) {
                newObj = _object_spread_props(_object_spread({}, newObj), _define_property({}, key, element));
            }
        }
    }
    return newObj;
}
/**
 * 生成一个拥有callback 的useState
 *
 * @export
 * @template T
 * @param {T} defaultState
 * @returns {[T, (state: Partial<T>) => void]}
 */ export default function useCallBackState(defaultState) {
    var setState = function setState(partialState, callback) {
        updateState(function(prevState) {
            var newState;
            // eslint-disable-next-line dot-notation
            if (defaultState["constructor"] === Object) {
                newState = concatObj(prevState, partialState);
            } else {
                newState = partialState;
            }
            if (callback) {
                callbackList.current.push(function() {
                    return callback(newState);
                });
            }
            return newState;
        });
    };
    var _useState = _sliced_to_array(useState(defaultState), 2), state = _useState[0], updateState = _useState[1];
    var callbackList = useRef([]);
    useEffect(function() {
        callbackList.current.forEach(function(value) {
            return value();
        });
        callbackList.current.splice(0, callbackList.current.length);
    }, [
        state
    ]);
    return [
        state,
        setState
    ];
}

 //# sourceMappingURL=useCallBackState.js.map